package me.sdevil507.org.repository;

import me.sdevil507.org.po.OrgUserPo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * Api用户repository
 *
 * @author sdevil507
 * created on 2019/12/15
 */
public interface OrgUserRepository extends JpaRepository<OrgUserPo, Long>, JpaSpecificationExecutor<OrgUserPo>, QuerydslPredicateExecutor<OrgUserPo> {

    /**
     * 根据手机号码查询用户信息
     *
     * @param phoneNumber 手机号码
     * @return 用户信息
     */
    OrgUserPo findByPhoneNumberAndDeletedIsFalse(String phoneNumber);

    /**
     * 根据姓名查询用户信息
     *
     * @param phoneNumber 手机号码
     * @return 用户信息
     */
    OrgUserPo findByRealNameAndDeletedIsFalse(String realName);

    /**
     * 根据邮箱查询用户信息
     *
     * @param mail 邮箱
     * @return 用户信息
     */
    OrgUserPo findByMailAndDeletedIsFalse(String mail);

    /**
     * 判断手机号码是否存在
     *
     * @param phoneNumber 手机号码
     * @return 执行反馈
     */
    boolean existsByPhoneNumberAndDeletedIsFalse(String phoneNumber);

    /**
     * 根据用户id列表查询出用户列表
     *
     * @param userIds 用户id列表
     * @return 用户列表
     */
    Set<OrgUserPo> findAllByIdIn(List<Long> userIds);

    /**
     * 根据手机号更改密码
     *
     * @param newPassword 新密码
     * @param phoneNumber 手机号
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query(value = "update OrgUserPo a set a.password= :newPassword where a.phoneNumber= :phoneNumber and a.deleted=false ")
    void updatePasswordByPhoneNumber(@Param("newPassword") String newPassword, @Param("phoneNumber") String phoneNumber);

    /**
     * 重置密码
     *
     * @param phoneNumber 手机号
     * @param newPassword 密码
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query(value = "update OrgUserPo a set a.password= :newPassword where a.phoneNumber= :phoneNumber")
    void resetPassword(@Param("phoneNumber") String phoneNumber, @Param("newPassword") String newPassword);

    /**
     * 重绑定手机号
     *
     * @param id          用户id
     * @param phoneNumber 手机号码
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query(value = "update OrgUserPo a set a.phoneNumber= :phoneNumber where a.id= :id")
    void bindPhoneNumber(@Param("id") Long id, @Param("phoneNumber") String phoneNumber);

    /**
     * 逻辑删除
     *
     * @param id 用户id
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query(value = "update OrgUserPo a set a.deleted=true where a.id=:id")
    void logicDelete(@Param("id") Long id);

    OrgUserPo findByRealNameAndExpertInfoIsNotNullAndDeletedIsFalse(String realName);
}
